#ifndef __APPCOMMONDEFINE_H
#define __APPCOMMONDEFINE_H

#include "fsl_common.h"
#include "FreeRTOS.h"
#include "task.h"


#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) < (b) ? (b) : (a))

#if (defined(__ICCARM__))

#define VARIABLE_ALIGN(var, alignbytes) 						SDK_PRAGMA(data_alignment = alignbytes) var
#define VARIABLE_NONCACHEABLE_SECTION(var) 						var @"NonCacheable"
#define VARIABLE_NONCACHEABLE_SECTION_ALIGN(var, alignbytes)	SDK_PRAGMA(data_alignment = alignbytes) var @"NonCacheable"


#elif defined(__CC_ARM) || defined(__ARMCC_VERSION)

#define VARIABLE_ALIGN(var, alignbytes) __attribute__((aligned(alignbytes))) var

#if(defined(__CC_ARM))
#define VARIABLE_NONCACHEABLE_SECTION(var) __attribute__((section("NonCacheable"), zero_init)) var
#define VARIABLE_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) \
    __attribute__((section("NonCacheable"), zero_init)) __attribute__((aligned(alignbytes))) var
#else
#define VARIABLE_NONCACHEABLE_SECTION(var) __attribute__((section(".bss.NonCacheable"))) var
#define VARIABLE_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) \
    __attribute__((section(".bss.NonCacheable"))) __attribute__((aligned(alignbytes))) var
#endif

#elif defined(__XCC__)

#define VARIABLE_NONCACHEABLE_SECTION(var) __attribute__((section("NonCacheable"))) var
#define VARIABLE_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) \
    __attribute__((section("NonCacheable"))) var __attribute__((aligned(alignbytes)))


#elif defined(__GNUC__)

#define VARIABLE_ALIGN(var, alignbytes) var __attribute__((aligned(alignbytes)))
#define VARIABLE_NONCACHEABLE_SECTION(var) __attribute__((section("NonCacheable,\"aw\",%nobits @"))) var
#define VARIABLE_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) __attribute__((section("NonCacheable,\"aw\",%nobits @"))) var __attribute__((aligned(alignbytes)))

#else

#error Toolchain not supported

#endif




/* SRE 压摆率选择 */
#define SRE_0_SLOW_SLEW_RATE                IOMUXC_SW_PAD_CTL_PAD_SRE(0)
#define SRE_1_FAST_SLEW_RATE                IOMUXC_SW_PAD_CTL_PAD_SRE(1)

/* 驱动能力配置，配置阻值的大小 */
#define DSE_0_OUTPUT_DRIVER_DISABLED        IOMUXC_SW_PAD_CTL_PAD_DSE(0)
/* R0 260 Ohm @ 3.3V, 150Ohm@1.8V, 240 Ohm for DDR */
#define DSE_1_R0_1                          IOMUXC_SW_PAD_CTL_PAD_DSE(1)
/* R0/2 */
#define DSE_2_R0_2                          IOMUXC_SW_PAD_CTL_PAD_DSE(2)
/* R0/3 */
#define DSE_3_R0_3                          IOMUXC_SW_PAD_CTL_PAD_DSE(3)
/* R0/4 */
#define DSE_4_R0_4                          IOMUXC_SW_PAD_CTL_PAD_DSE(4)
/* R0/5 */
#define DSE_5_R0_5                          IOMUXC_SW_PAD_CTL_PAD_DSE(5)
/* R0/6 */
#define DSE_6_R0_6                          IOMUXC_SW_PAD_CTL_PAD_DSE(6)
/* R0/7 */
#define DSE_7_R0_7                          IOMUXC_SW_PAD_CTL_PAD_DSE(7)

/* SPEED 带宽配置 */
#define SPEED_0_LOW_50MHz                   IOMUXC_SW_PAD_CTL_PAD_SPEED(0)
#define SPEED_1_MEDIUM_100MHz               IOMUXC_SW_PAD_CTL_PAD_SPEED(1)
#define SPEED_2_MEDIUM_100MHz               IOMUXC_SW_PAD_CTL_PAD_SPEED(2)
#define SPEED_3_MAX_200MHz                  IOMUXC_SW_PAD_CTL_PAD_SPEED(3)

/* ODE 是否使用开漏模��*/
#define ODE_0_OPEN_DRAIN_DISABLED           IOMUXC_SW_PAD_CTL_PAD_ODE(0)
#define ODE_1_OPEN_DRAIN_ENABLED            IOMUXC_SW_PAD_CTL_PAD_ODE(1)

/* PKE 是否使能保持器或上下拉功��*/
#define PKE_0_PULL_KEEPER_DISABLED          IOMUXC_SW_PAD_CTL_PAD_PKE(0)
#define PKE_1_PULL_KEEPER_ENABLED           IOMUXC_SW_PAD_CTL_PAD_PKE(1)

/* PUE 选择使用保持器还是上下拉 */
#define PUE_0_KEEPER_SELECTED               IOMUXC_SW_PAD_CTL_PAD_PUE(0)
#define PUE_1_PULL_SELECTED                 IOMUXC_SW_PAD_CTL_PAD_PUE(1)

/* PUS 上下拉配��*/
#define PUS_0_100K_OHM_PULL_DOWN            IOMUXC_SW_PAD_CTL_PAD_PUS(0)
#define PUS_1_47K_OHM_PULL_UP               IOMUXC_SW_PAD_CTL_PAD_PUS(1)
#define PUS_2_100K_OHM_PULL_UP              IOMUXC_SW_PAD_CTL_PAD_PUS(2)
#define PUS_3_22K_OHM_PULL_UP               IOMUXC_SW_PAD_CTL_PAD_PUS(3)

/* HYS 滞后功能 */
#define HYS_0_HYSTERESIS_DISABLED           IOMUXC_SW_PAD_CTL_PAD_HYS(0)
#define HYS_1_HYSTERESIS_ENABLED            IOMUXC_SW_PAD_CTL_PAD_HYS(1)


#define PIN_CONFIG_OUTPUT (SRE_0_SLOW_SLEW_RATE| \
	DSE_6_R0_6| \
	SPEED_2_MEDIUM_100MHz| \
	ODE_0_OPEN_DRAIN_DISABLED| \
	PKE_0_PULL_KEEPER_DISABLED| \
	PUE_0_KEEPER_SELECTED| \
	PUS_0_100K_OHM_PULL_DOWN| \
	HYS_0_HYSTERESIS_DISABLED)
	
#define PIN_CONFIG_OUTPUT_OPEN (SRE_0_SLOW_SLEW_RATE| \
	DSE_6_R0_6| \
	SPEED_2_MEDIUM_100MHz| \
	ODE_1_OPEN_DRAIN_ENABLED| \
	PKE_0_PULL_KEEPER_DISABLED| \
	PUE_0_KEEPER_SELECTED| \
	PUS_0_100K_OHM_PULL_DOWN| \
	HYS_0_HYSTERESIS_DISABLED)


#define PIN_CONFIG_INPUT (SRE_0_SLOW_SLEW_RATE| \
	DSE_6_R0_6| \
	SPEED_2_MEDIUM_100MHz| \
	ODE_0_OPEN_DRAIN_DISABLED| \
	PKE_1_PULL_KEEPER_ENABLED| \
	PUE_1_PULL_SELECTED| \
	PUS_3_22K_OHM_PULL_UP| \
	HYS_0_HYSTERESIS_DISABLED)


#define appTask_CreateTask_PRIORITY             0
#define appTask_CreateTask_STACK_DEPTH			(1024 * 8)
#define appTask_CreateTask_NANE                 NULL

#define appTask_SystemStatus_PRIORITY           0
#define appTask_SystemStatus_STACK_DEPTH        (configMINIMAL_STACK_SIZE)
#define appTask_SystemStatus_NANE               NULL

#define appTask_USBTransfe_PRIORITY             1
#define appTask_USBTransfe_STACK_DEPTH          (configMINIMAL_STACK_SIZE)
#define appTask_USBTransfe_NANE                 NULL

#define appTask_AudioProcessed_PRIORITY         4
#define appTask_AudioProcessed_STACK_DEPTH      (1024 * 4)
#define appTask_AudioProcessed_NANE             NULL

#define CFG_I2S_CHANNEL                         2u
#define CFG_I2S_FRAME_SIZE                      128u
#define CFG_I2S_SAMPLERATE                      44100u
#define CFG_I2S_WORDWIDTH                       24u
#define CFG_DATA_DEAL_FLOAT                     1
#define CFG_BYPASS                              1
            
#if CFG_I2S_WORDWIDTH == 16u
	typedef int16_t                             AUDIO_VALUE_TYPE;
	#define AUDIO_VALUE_BYTES                   2u
	#define AUDIO_VALUE_MAX                     (AUDIO_VALUE_TYPE)32767
	#define AUDIO_VALUE_MIN                     (AUDIO_VALUE_TYPE)-32768
#elif CFG_I2S_WORDWIDTH == 24u
	typedef int32_t                             AUDIO_VALUE_TYPE;
	#define AUDIO_VALUE_BYTES                   4u
	#define AUDIO_VALUE_MAX                     (AUDIO_VALUE_TYPE)8388607
	#define AUDIO_VALUE_MIN                     (AUDIO_VALUE_TYPE)-8388608
#elif CFG_I2S_WORDWIDTH == 32u
	typedef int32_t                             AUDIO_VALUE_TYPE;
	#define AUDIO_VALUE_BYTES                   4u
	#define AUDIO_VALUE_MAX                     (AUDIO_VALUE_TYPE)2147483647
	#define AUDIO_VALUE_MIN                     (AUDIO_VALUE_TYPE)-2147483648
#else
	#error "CFG_I2S_WORDWIDTH define error,must set to 16,24,32"
#endif

#define SAMPLE_BYTES                            (AUDIO_VALUE_BYTES * CFG_I2S_CHANNEL)
#define FRAME_BUFFER_BYTES                      (SAMPLE_BYTES * CFG_I2S_FRAME_SIZE)

typedef enum
{
    status_success = 0,
    status_usb_send_busy,
    status_usb_send_fail,
    status_emmc_init_fail,
    status_emmc_write_fail,
    status_emmc_read_fail,
    status_fifo_size_error,
}appStatus_t;

#endif

